package com.hxci.hospital.controller;

import com.hxci.hospital.pojo.Doctor;
import com.hxci.hospital.pojo.Patient;
import com.hxci.hospital.service.DoctorService;
import com.hxci.hospital.service.PatientService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/hospital/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private PatientService patientService;

    // 医生登录页面
    @RequestMapping("/login")
    public String login() {
        return "hospital/doctor/login";
    }

    // 医生登录验证
    @ResponseBody
    @RequestMapping("/tologin")
    public Integer tologin(@RequestParam("username") String username, 
                          @RequestParam("password") String password,
                          HttpSession session) {
        Doctor doctor = doctorService.queryByUsernameAndPassword(username, password);
        if (doctor != null) {
            session.setAttribute("doctor", doctor);
            return 1;
        }
        return 0;
    }

    // 医生主页
    @RequestMapping("/index")
    public String index() {
        return "hospital/doctor/index";
    }

    // 获取当前医生的患者列表
    @ResponseBody
    @RequestMapping("/myPatients")
    public List<Patient> myPatients(HttpSession session) {
        Doctor doctor = (Doctor) session.getAttribute("doctor");
        if (doctor != null) {
            // 根据角色返回不同的患者列表
            if ("2".equals(doctor.getRole())) {
                // 管理员可以查看所有患者
                return patientService.queryAll();
            } else if ("1".equals(doctor.getRole())) {
                // 科室主任可以查看本科室所有患者
                return patientService.queryByDepartment(doctor.getDepartment());
            } else {
                // 普通医生只能查看自己的患者
                return patientService.queryByDoctorId(doctor.getId());
            }
        }
        return null;
    }

    // 更新患者状态
    @ResponseBody
    @RequestMapping("/updatePatientStatus")
    public Integer updatePatientStatus(@RequestParam("patientId") Integer patientId,
                                     @RequestParam("status") Integer status) {
        return patientService.updateStatus(patientId, status);
    }

    // 查询所有医生
    @ResponseBody
    @RequestMapping("/queryAll")
    public List<Doctor> queryAll() {
        return doctorService.queryAll();
    }

    // 根据科室查询医生
    @ResponseBody
    @RequestMapping("/queryByDepartment")
    public List<Doctor> queryByDepartment(@RequestParam("department") String department, HttpSession session) {
        Doctor currentDoctor = (Doctor) session.getAttribute("doctor");
        if (currentDoctor != null) {
            if ("2".equals(currentDoctor.getRole())) {
                // 管理员可以查看任何科室的医生
                return doctorService.queryByDepartment(department);
            } else if ("1".equals(currentDoctor.getRole())) {
                // 科室主任可以查看本科室的所有医生
                if (currentDoctor.getDepartment().equals(department)) {
                    return doctorService.queryByDepartment(department);
                }
            } else {
                // 普通医生只能查看本科室的普通医生
                if (currentDoctor.getDepartment().equals(department)) {
                    return doctorService.queryByDepartmentAndRole(department, "0");
                }
            }
        }
        return null;
    }
}
